package com.cuitrip.business.setting;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.app.MainApplication;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.DebugUtil;
import com.cuitrip.util.I118Util;
import com.cuitrip.util.ResourceUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.utils.Utils;

import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * Created on 7/16.
 */
public class AboutActivity extends CustomUiFragmentActivity {

    private int mLogoClickTimes = 0;
    private long mLogoClickFirstTime = 0;
    public static final long mLogoClickInterval = 10000;
    private ImageView logoView;
    @Bind(R.id.itemWebSite)
    ItemLayout itemWebsite;
    @Bind(R.id.itemWechat)
    ItemLayout itemWechat;
    @Bind(R.id.itemFacebook)
    ItemLayout itemFacebook;
    @Bind(R.id.itemInstagram)
    ItemLayout itemInstagram;
    @Bind(R.id.itemTumblr)
    ItemLayout itemTumblr;
    //    Facebook：https://www.facebook.com/tripinsiders
    public static final String urlFacebook = "https://www.facebook.com/tripinsiders";
    public static final String urlInstagram = "https://www.instagram.com/tripinsiders/";
    public static final String urlTumblr = "http://tripinsiders.tumblr.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.ct_about);
        ButterKnife.bind(this);
        boolean showWechat = I118Util.getLocale().equals(Locale.CHINA);
        itemWechat.setVisibility(showWechat ? View.VISIBLE : View.GONE);
        itemFacebook.setVisibility(showWechat ? View.GONE : View.VISIBLE);
        itemTumblr.setVisibility(showWechat ? View.GONE : View.VISIBLE);
        itemInstagram.setVisibility(showWechat ? View.GONE : View.VISIBLE);
        itemWechat.setleftText(Html.fromHtml("微信公众号：  <font color='"+ ResourceUtil.getThemeColor()+"'>cuitrip</font>"));
        itemWebsite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startBrowser(AboutActivity.this, getString(R.string.website_tripinsider));
            }
        });
        itemFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Utils.startBrowser(AboutActivity.this,urlFacebook);
                Utils.jump(AboutActivity.this, "fb://page/1657333287812494", "com.facebook.katana", urlFacebook);
            }
        });
        itemInstagram.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Utils.startBrowser(AboutActivity.this,urlInstagram);
                Utils.jump(AboutActivity.this, "instagram://user?username=tripinsiders", "com.instagram.android", urlInstagram);
            }
        });
        itemTumblr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Utils.startBrowser(AboutActivity.this,urlTumblr);
                Utils.startBrowser(AboutActivity.this, "https://tripinsiders.wordpress.com/");
//                Utils.jump(AboutActivity.this,"tumblr://x-callback-url/blog?blogName=tripinsiders","com.tumblr",urlTumblr);
            }
        });
    }

    @Override
    public void initView() {
        ((TextView) findViewById(R.id.ct_version)).setText("V" + MainApplication.getInstance().getVersionName());
        logoView = (ImageView) findViewById(R.id.logo);
        logoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long secondTime = System.currentTimeMillis();
                if (secondTime - mLogoClickFirstTime <= mLogoClickInterval) {
                    mLogoClickTimes++;
                } else {
                    mLogoClickTimes = 0;
                }
                mLogoClickFirstTime = secondTime;
            }
        });

        logoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mLogoClickTimes <= 3) {
                    return true;
                }
                mLogoClickTimes = 0;
                DebugUtil.showTestDialog(AboutActivity.this);
                return true;
            }
        });
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarMiddleAreaName = getString(R.string.setting_about);
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }
}
